package com.qyer.commons.model;

import static com.qyer.commons.utils.CommonUtils.cast2Int;
import static com.qyer.commons.utils.CommonUtils.cast2String;

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * User: Z J Wu Date: 2016/04/11 Time: 17:59 Package: com.qyer.commons.model
 */
public class User {

  public static final User UNKNOWN_USER = new User(0, "");

  @JsonIgnore
  private int uid;

  @JsonIgnore
  private String uniqueId;

  private User() {
  }

  private User(int uid, String uniqueId) {
    this.uid = uid;
    this.uniqueId = uniqueId;
  }

  public int getUid() {
    return uid;
  }

  public void setUid(int uid) {
    this.uid = uid;
  }

  public String getUniqueId() {
    return uniqueId;
  }

  public void setUniqueId(String uniqueId) {
    this.uniqueId = uniqueId;
  }

  public static User buildUser(HttpServletRequest request) {
    int uid = cast2Int(request.getParameter("uid"));

    String uniqueId = cast2String(request.getParameter("guid"));
    if (StringUtils.isBlank(uniqueId)) {
      uniqueId = cast2String(request.getParameter("device_id"));
    }
    return (uid <= 0 && StringUtils.isBlank(uniqueId)) ? UNKNOWN_USER : new User(uid, uniqueId);
  }
}
